package com.topvision.camerasdk.camera;

import android.content.Context;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.os.Build;
import android.util.Log;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.topvision.commomsdk.uitl.AndroidUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * User: jack(jackgu@topvision-cv.com)
 * Date: 2016-11-21
 * Time: 15:25
 */

/**
 * 拍照回調方法
 *
 * @author jack
 *         create at 2016/11/21 14:47
 */
public class TakePhotoCallback implements Camera.PictureCallback {
    //缩略图的大小
    public static final int DISPLAY_WIDTH = 400;
    public static final int DISPLAY_HEIGHT = 400;
    Context context;
    ImageView albumIv;
    Bitmap currPhoto;
    int picWidth;
    int picHeight;
    CameraActivity cameraActivity;

    public TakePhotoCallback(CameraActivity cameraActivity, Context context, ImageView albumIv, int picWidth, int picHeight) {
        this.context = context;
        this.albumIv = albumIv;
        this.picWidth = picWidth;
        this.picHeight = picHeight;
        this.cameraActivity = cameraActivity;
    }

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        try {
            // 将图片字节数据保存在bundle当中，实现数据交换
            String preFixFileName = String.valueOf(new Date().getTime());
            // 保存图片到sd卡中
            File file = saveToSDCard(context, data, preFixFileName);
            // 生成缩略图
            currPhoto = AndroidUtils.decodeBitmap(file.getPath(), picWidth, picHeight,
                    DISPLAY_WIDTH, DISPLAY_HEIGHT);
            //缩略图的绝对路径
            String thumbnailName = CameraConstants.THUMBNAIL_FOLDER + File.separator + preFixFileName + "-" + CameraActivity.THUMBNAIL_NAME + ".jpg";
            // 保存缩略图
            AndroidUtils.saveThumbnail(currPhoto, thumbnailName);
            Picasso.with(context).load("file://" + thumbnailName).into(albumIv);
            if (Build.VERSION.SDK_INT != Build.VERSION_CODES.KITKAT) {
                AndroidUtils.fileScan(context, file.getPath());
                AndroidUtils.fileScan(context, thumbnailName);
            }
            //startPreview是异步的,所以 cameraActivity.setSafeToTakePicture(true);有时候不一定真正开启预览了
            camera.startPreview();

        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.e("tvLog", String.valueOf(System.currentTimeMillis() - CameraActivity.takePicTime));
        //拍照过程结束,允许下次继续拍照
        cameraActivity.setTakingPhoto(false);
    }

    /**
     * 将拍下来的照片存放在SD卡中
     *
     * @param data
     * @throws IOException
     */
    public static File saveToSDCard(Context context, byte[] data, String fileName) throws IOException {
        String filename = fileName + ".jpg";
        File fileFolder = new File(CameraConstants.TAKE_PHOTO_FOLDER);
        System.out.println(fileFolder);
        if (!fileFolder.exists()) {
            fileFolder.mkdirs();
            AndroidUtils.fileScan(context, fileFolder.getPath());
        }
        //将byte数组写入到某个文件当中,
        File jpgFile = new File(fileFolder, filename);
        FileOutputStream outputStream = new FileOutputStream(jpgFile); // 文件输出流
        outputStream.write(data); // 写入sd卡中
        outputStream.close(); // 关闭输出流
        return jpgFile;
    }

    public Bitmap getCurrPhoto() {
        return currPhoto;
    }

    public void setCurrPhoto(Bitmap currPhoto) {
        this.currPhoto = currPhoto;
    }


}
